﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.SQLite;
using System.Data;
using Pub;
using JT_DBTree.pub;

namespace LZQXT.Test.Forms.DB
{
    public partial class FDBInit : Form
    {

        public static string S_Con;

        public string DBCon;
        public string access_con;

        //public CDBTree_Access dbCon = new CDBTree_Access();
        public CDBTree dbCon = new CDBTree();

        public FDBInit()
        {
            InitializeComponent();
        }

        private void FDBInit_Load(object sender, EventArgs e)
        {
            lstBox_ConName.Dock = DockStyle.Fill;
            panel2.Dock = DockStyle.Fill;
            tbox_ConStr.Dock = DockStyle.Fill;

            db_init();
        }

        public void db_init()
        {
            lstBox_Server.Items.Clear();
            for (int i = 0; i < dbCon.lstDBInf.Count; i++)
            {
                lstBox_Server.Items.Add(dbCon.lstDBInf[i].sServ);
            }

            if (lstBox_Server.Items.Count > 0) lstBox_Server.SelectedIndex = 0;
        }

        private void FDBInit_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        /// <summary>
        /// 选择连接类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstBox_Server_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                fillDBInf();

                lstBox_ConName.Items.Clear();
                List<string> lstDBNamS = new List<string>();
                lstDBNamS = dbCon.getDBNam(lstBox_Server.Text);
                for (int i = 0; i < lstDBNamS.Count; i++)
                {
                    lstBox_ConName.Items.Add(lstDBNamS[i]);
                }

                if (lstBox_ConName.Items.Count > 0) lstBox_ConName.SelectedIndex = 0;

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        private void lstBox_ConName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sServ = lstBox_Server.Text;
            string sD_B_ = lstBox_ConName.Text;
            string strDBCon = dbCon.getDBCon(sServ, sD_B_);
            tbox_ConStr.Text = strDBCon;

            fillDBInf();
        }

        public void fillDBInf()
        {
            string sServ = lstBox_Server.Text;
            FDBTree.fdbinit_add.s_ID = dbCon.getDBInfo(sServ)[0].sID;
            FDBTree.fdbinit_add.tboxDB.Text = dbCon.getDBInfo(sServ)[0].sD_B_;
            FDBTree.fdbinit_add.tboxServer.Text = dbCon.getDBInfo(sServ)[0].sServ;
            FDBTree.fdbinit_add.tboxUser.Text = dbCon.getDBInfo(sServ)[0].sUser;
            FDBTree.fdbinit_add.tboxPass.Text = dbCon.getDBInfo(sServ)[0].sPass;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FDBTree.fdbinit_add.i_Typ = 1;
            FDBTree.fdbinit_add.ShowDialog();

            db_init();
        }

        private void btnEdt_Click(object sender, EventArgs e)
        {
            FDBTree.fdbinit_add.i_Typ = 2;
            FDBTree.fdbinit_add.ShowDialog();

            db_init();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string strSQL = "DELETE FROM T_DBCon WHERE ID = " + FDBTree.fdbinit_add.s_ID;
            if (CPub.MsgBox(strSQL, "删除数据"))
            {
                dbCon.cmd1(strSQL);
            }

            db_init();
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            db_init();
        }


    }
    /*
    public class DBCon
    {

        public string S_Con;

        int i_ID;
        public string access_con;
        public SQLiteCommand cm;
        public SQLiteConnection cn;
        //public static List<SqlConnection> lst_cn = new List<SqlConnection>();
        //public static List<string> lstConKey = new List<string>();
        //public static List<string> lstConVal = new List<string>();

        public SQLiteDataAdapter da;
        public int ds_count = 0;
        public SQLiteParameter[] pars;
        public List<SQLiteParameter> pars2;
        public string sqlServer_con;
        public string s_sql = "";

        public struct DBInfo
        {
            public string sID;
            public string sServ;
            public string sPass;
            public string sUser;
            public string sD_B_;
            public string sDBCon;
        }

        public List<DBInfo> lstDBInf = new List<DBInfo>();

        public DBCon()
        {
            try
            {
                db_init();
            }
            catch (Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }
        }

        public void db_init()
        {
            try
            {
                string sDBCon = "data source=" + Application.StartupPath + "\\DBCon.db";
                cn = new SQLiteConnection(sDBCon);

                DataSet ds1 = sel1("SELECT * FROM T_DBCon ORDER BY [Last] DESC");
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        DBInfo dbInf_Tem = new DBInfo();
                        dbInf_Tem.sID = dr1["ID"].ToString();
                        dbInf_Tem.sServ = dr1["Server"].ToString();
                        dbInf_Tem.sD_B_ = dr1["DataBase"].ToString();
                        dbInf_Tem.sUser = dr1["User"].ToString();
                        dbInf_Tem.sPass = dr1["Pass"].ToString();

                        lstDBInf.Add(dbInf_Tem);
                    }
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        public string getDBCon(DBInfo dbInf)
        {
            return "Data Source=" + dbInf.sServ + ";Initial Catalog=" + dbInf.sD_B_.Split(';')[0]
                + ";Persist Security Info=True;User ID=" + dbInf.sUser + ";Password=" + dbInf.sPass;
        }

        public string getDBCon(DBInfo dbInf, string sD_B_)
        {
            return "Data Source=" + dbInf.sServ + ";Initial Catalog=" + sD_B_
                + ";Persist Security Info=True;User ID=" + dbInf.sUser + ";Password=" + dbInf.sPass;
        }

        public string getDBCon(string sServ, string sD_B_)
        {
            DBInfo dbInf = new DBInfo();
            dbInf = getDBInfo(sServ);
            return "Data Source=" + dbInf.sServ + ";Initial Catalog=" + sD_B_
                + ";Persist Security Info=True;User ID=" + dbInf.sUser + ";Password=" + dbInf.sPass;
        }

        public DataSet sel1(string sql)
        {
            try
            {
                this.s_sql = sql;
                cn.Open();
                da = new SQLiteDataAdapter(sql, cn);
                DataSet ds = new DataSet();

                da.Fill(ds);
                SQLiteCommandBuilder sqlCb = new SQLiteCommandBuilder(da);

                cn.Close();

                //da.FillSchema(ds, SchemaType.Mapped);
                try
                {
                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        ds_count = 0;
                    }

                    ds_count = ds.Tables[0].Rows.Count;
                }
                catch (System.Exception ex1)
                {
                    ds_count = 0;
                    CDebug.Send("返回结果个数出错", ex1);
                }

                return ds;

            }
            catch (Exception ex)
            {
                if (cn.State != System.Data.ConnectionState.Closed) cn.Close();

                CDebug.Send(sql + "; " + ex.ToString());
                CDebug.Send("sql语句错误", ex);
                ds_count = 0;
                return null;
            }
        }

        public int cmd1(string sql)
        {
            cn.Open();
            cm = new SQLiteCommand();
            cm.Connection = cn;

            cm = new SQLiteCommand(sql, cn);

            int i = 0;
            try
            {
                i = cm.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            cn.Close();
            return i;

        }

        public DBInfo getDBInfo(string sServ)
        {
            DBInfo dbInf = new DBInfo();
            for (int i = 0; i < lstDBInf.Count; i++)
            {
                if (lstDBInf[i].sServ == sServ)
                {
                    dbInf = lstDBInf[i];
                    break;
                }
            }

            return dbInf;
        }

        public List<string> getDBNam(string sServ)
        {
            List<string> lstDBNam = new List<string>();
            DBInfo dbInf = new DBInfo();
            dbInf = getDBInfo(sServ);
            string[] sDBNamS = dbInf.sD_B_.Split(';');
            for (int i = 0; i < sDBNamS.Length; i++)
            {
                lstDBNam.Add(sDBNamS[i]);
            }

            return lstDBNam;
        }
    }
     * */
}
